<template>
  <div class="login-container">
    <div class="login-content">
      <h1 class="title">校园智慧竞赛平台</h1>
      
      <div class="login-form">
        <div class="form-item">
          <label>工号：</label>
          <input 
            v-model="loginForm.employeeId" 
            type="text" 
            class="form-input"
            placeholder="请输入工号"
          />
        </div>
        
        <div class="form-item">
          <label>密码：</label>
          <input 
            v-model="loginForm.password" 
            type="password" 
            class="form-input"
            placeholder="请输入密码"
          />
        </div>
        
        <div class="button-group">
          <button class="register-btn" @click="handleRegister">注册</button>
          <button class="login-btn" @click="handleLogin">登录</button>
        </div>
      </div>
      
      <div class="forgot-password">
        <a href="#" @click.prevent="handleForgotPassword">忘记密码?</a>
      </div>
    </div>

    <!-- 自定义弹窗 -->
    <div v-if="showModal" class="modal-overlay" @click="closeModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>{{ modalTitle }}</h3>
          <button class="close-btn" @click="closeModal">×</button>
        </div>
        <div class="modal-body">
          <div class="icon-wrapper">
            <div class="info-icon">!</div>
          </div>
          <p class="modal-text">{{ modalText }}</p>
          <p v-if="modalContactInfo" class="contact-info">{{ modalContactInfo }}</p>
        </div>
        <div class="modal-footer">
          <button class="confirm-btn" @click="closeModal">我知道了</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { teacherLoginService, getTeacherInfoService } from '@/api/teacher.js'
import { useTokenStore } from '@/stores/token.js'
import { useTeacherStore } from '@/stores/teacher.js'

const router = useRouter()
const tokenStore = useTokenStore()
const teacherStore = useTeacherStore()

// 表单数据
const loginForm = reactive({
  employeeId: '',
  password: ''
})

// 弹窗控制
const showModal = ref(false)
const modalTitle = ref('')
const modalText = ref('')
const modalContactInfo = ref('')

// 显示弹窗
const showCustomModal = (title, text, contactInfo = '') => {
  modalTitle.value = title
  modalText.value = text
  modalContactInfo.value = contactInfo
  showModal.value = true
}

// 登录处理
const handleLogin = async () => {
  if (!loginForm.employeeId || !loginForm.password) {
    showCustomModal('提示', '请输入工号和密码')
    return
  }
  
  try {
    // 调用登录接口
    const result = await teacherLoginService(loginForm)
    
    if (result.code !== 200) {
      showCustomModal('登录失败', result.msg || '登录失败，请检查工号和密码')
      return
    }
    
    // 存储token
    tokenStore.setToken(result.data.token)
    
    // 获取教师详细信息
    try {
      const teacherInfoResult = await getTeacherInfoService()
      if (teacherInfoResult.code === 200) {
        teacherStore.setTeacherInfo(teacherInfoResult.data)
      }
    } catch (error) {
      console.warn('获取教师信息失败:', error)
    }
    
    // 根据审核状态处理跳转
    const auditStatus = result.data.auditStatus
    if (auditStatus === 0) {
      showCustomModal('提示', '您的账号正在审核中，审核通过后方可使用系统功能', '请耐心等待管理员审核')
      return
    } else if (auditStatus === 2) {
      showCustomModal('提示', '您的账号审核未通过，请联系管理员', '如有疑问请联系相关负责人')
      return
    }
    
    // 审核通过，跳转到主页
    showCustomModal('登录成功', '欢迎进入校园智慧竞赛平台')
    setTimeout(() => {
      router.push('/home')
    }, 1500)
    
  } catch (error) {
    console.error('登录失败:', error)
    showCustomModal('登录失败', error.msg || '登录失败，请检查网络连接')
  }
}

// 注册处理
const handleRegister = () => {
  router.push('/register')
}

// 忘记密码处理
const handleForgotPassword = () => {
  showCustomModal('密码重置', '请联系管理员重置密码', '联系电话：135****5111')
}

// 关闭弹窗
const closeModal = () => {
  showModal.value = false
}
</script>

<style scoped lang="scss">
@import '@/assets/login.css';
</style>
